package 线程.线程的安全问题;

import java.io.*;

public class MySelf extends Thread{
    //类的所有对象都共享这个数据
    static int ticket=0;
    //锁对象（唯一）
    static Object obj=new Object();
    @Override
    public void run() {
        while (true){
            synchronized (obj){
                if (ticket<100){
                    try {
                        sleep(1000);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                    ticket++;
                    System.out.println(getName()+",正在卖第"+ticket+"张票");
                }else{
                    break;
                }
            }
        }
    }
}
